﻿@namespace Blazorise.Docs.Docs.Examples

<DataGrid TItem="Employee"
          @ref="dataGridRef"
          Data="@employeeList"
          DetailRowTrigger="@(e => DisplayDetailRow(e.Item))"
          DetailRowStartsVisible="false"
          Responsive>
    <DataGridColumns>
        <DataGridCommandColumn />
        <DataGridColumn TItem=Employee >
            <DisplayTemplate>
                @if ( DisplayDetailRow( context ) )
                {
                    <Button>
                        <Icon Name="@(dataGridRef.GetRowInfo(context).DetailRowVisible ? IconName.ExpandLess : IconName.ExpandMore)"/>
                    </Button>
                }
            </DisplayTemplate>
        </DataGridColumn>
        <DataGridColumn Field="@nameof(Employee.FirstName)" Caption="First Name" />
    </DataGridColumns>
    <DetailRowTemplate>
        @{
            var salaries = context.Salaries;

            <DataGrid TItem="Salary"
                      Data="salaries"
                      Sortable="false"
                      ShowCaptions="false">
                <DataGridCommandColumn />
                <DataGridDateColumn Field="@nameof(Salary.Date)" Caption="Date" />
                <DataGridNumericColumn Field="@nameof(Salary.Total)" Caption="Total" />
            </DataGrid>
        }
    </DetailRowTemplate>
</DataGrid>

@code{
    [Inject]
    public EmployeeData EmployeeData { get; set; }
    private List<Employee> employeeList;
    DataGrid<Employee> dataGridRef;

    bool DisplayDetailRow(Employee employee) => employee.Salaries?.Count > 0;

    protected override async Task OnInitializedAsync()
    {
        employeeList = await EmployeeData.GetDataAsync();
        await base.OnInitializedAsync();
    }
}